#include "editdialog.h"

EditDialog::EditDialog()
{
    setModal(true);
    setGeometry(200,200,400,150);
    //setMaximumSize(400,150);
    //setMinimumSize(400,150);
    createWidgets();
}

void EditDialog::createWidgets()
{
    QVBoxLayout* vlay = new QVBoxLayout();

    QHBoxLayout* namelay = new QHBoxLayout();
    QHBoxLayout* addrlay = new QHBoxLayout();
    QHBoxLayout* taglay = new QHBoxLayout();

    QLabel* namelbl = new QLabel("Name:    ");
    QLabel* addrlbl = new QLabel("Address: ");
    QLabel* taglbl =  new QLabel("Tags:    ");

    namelbl->setMinimumWidth(50);
    namelbl->setMaximumWidth(50);
    addrlbl->setMinimumWidth(50);
    addrlbl->setMaximumWidth(50);
    taglbl->setMinimumWidth(50);
    taglbl->setMaximumWidth(50);


    addrlbl->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    taglbl->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    name = new QLineEdit();
    address = new QLineEdit();
    tags = new QLineEdit();

    namelay->addWidget(namelbl);
    namelay->addWidget(name);

    addrlay->addWidget(addrlbl);
    addrlay->addWidget(address);

    taglay->addWidget(taglbl);
    taglay->addWidget(tags);

    confirm = new QPushButton("Ok");
    cancel = new QPushButton("Cancel");
    confirm->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    cancel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);


    //thumb = new QImage();
    //thumb->load(":images/youtube.jpg");
    thumbLbl = new QLabel();
    thumbLbl->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    thumbLbl->setMaximumSize(200,200);
    vlay->addWidget(thumbLbl,0,Qt::AlignHCenter);

    vlay->addLayout(namelay);
    vlay->addLayout(addrlay);
    vlay->addLayout(taglay);

    QHBoxLayout* buttonlay = new QHBoxLayout();

    buttonlay->addWidget(confirm,Qt::AlignLeft);
    buttonlay->addWidget(cancel,Qt::AlignRight);

    vlay->addLayout(buttonlay);

    setLayout(vlay);

    connect(confirm,SIGNAL(clicked()),this,SLOT(accept()));
    connect(cancel,SIGNAL(clicked()),this,SLOT(reject()));
    connect(address,SIGNAL(textChanged(QString)),this,SLOT(loadSnapshot(QString)));

    //wt = new WebThumbnail("http://www.google.com");
    //connect(wt,SIGNAL(snapdone(bool)),this,SLOT(loadLabel(bool)));
}

void EditDialog::loadSnapshot(QString url)
{
    QRegExp p("(https?://)([a-z0-9]|-)+([.]([a-z0-9]|-)+)+/");
    if(p.exactMatch(url))
    {
        wt = new WebThumbnail(url);
        connect(wt,SIGNAL(snapdone(bool)),this,SLOT(loadLabel(bool)));

        thumbLbl->setMovie(new QMovie(":/images/loading.gif"));
        thumbLbl->movie()->setScaledSize(QSize(50,50));
        thumbLbl->movie()->start();
        thumbLbl->show();
    }
}

void EditDialog::loadLabel(bool done)
{
    pix = wt->getThumbnail();
    qDebug()<<"HELLO!";

    thumbLbl->setScaledContents(true);
    thumbLbl->setPixmap(pix);
    delete wt;
    delete thumbLbl->movie();
}

